/*
 * @Description: 基础输入框
 * @Version: V1.0.0
 * @Author: 黄鑫
 * @Date: 2025-05-19
 * @LastEditors: 黄鑫
 * @LastEditTime: 2025-07-30
 * @FilePath: BaseTextField.dart
 * @Remarks:
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:hx_base/utils/HXUtils.dart';

class BaseTextField extends StatefulWidget {
  /// 初始值
  final String text;

  /// 提示语
  final String hintText;

  /// 默认 EdgeInsets.fromLTRB(12, 4, 12, 4)
  final EdgeInsetsGeometry? contentPadding;

  /// 输入类型
  final TextInputType? keyboardType;

  // 是否只读
  final bool? readOnly;

  /// 文本对齐
  final TextAlign? textAlign;

  /// 是否密码类型
  final bool isPws;

  /// 是否显示计数器
  final bool isShowCounterText;

  /// 输入框 style
  final TextStyle? textStyle;

  /// 提示语 style
  final TextStyle? hintStyle;

  /// 最大行
  final int? maxLines;

  /// 最大长度
  final int? maxLength;

  /// 自定义装饰
  final InputDecoration? inputDecoration;

  /// 文本操作控制器（复制剪切等）
  final TextSelectionControls? selectionControls;

  /// 返回值
  final Function(String value) callBackValue;

  const BaseTextField({
    super.key,
    required this.text,
    required this.callBackValue,
    this.hintText = "",
    this.textAlign,
    this.contentPadding,
    this.keyboardType = TextInputType.text,
    this.isPws = false,
    this.isShowCounterText = false,
    this.textStyle,
    this.hintStyle,
    this.readOnly = false,
    this.maxLines = 1,
    this.maxLength,
    this.inputDecoration,
    this.selectionControls,
  });

  @override
  State<BaseTextField> createState() => _BaseTextFieldState();
}

class _BaseTextFieldState extends State<BaseTextField> {
  late final TextEditingController _controller;
  final FocusNode _focusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController(text: widget.text);
  }

  @override
  void dispose() {
    _controller.dispose();
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    _controller.text = widget.text;
    return TextField(
      focusNode: _focusNode,
      readOnly: widget.readOnly ?? false,
      controller: _controller,
      keyboardType: widget.keyboardType ?? TextInputType.text,
      obscureText: widget.isPws,
      style: widget.textStyle,
      onChanged: (text) {
        widget.callBackValue(text);
      },
      onTap: () async {
        HXUtils.hideKeyboard();
        await Future.delayed(const Duration(milliseconds: 100));
        FocusScope.of(Get.context!).requestFocus(_focusNode);
      },
      textAlign: widget.textAlign ?? TextAlign.start,
      selectionControls: widget.selectionControls,
      maxLength: widget.maxLength,
      maxLines: widget.maxLines,
      decoration: widget.inputDecoration ??
          InputDecoration(
            counterText: widget.isShowCounterText ? null : "",
            hintText: widget.hintText,
            hintStyle: widget.hintStyle ??
                TextStyle(color: const Color(0xFF9FA1A9), fontSize: 14.sp),
            contentPadding: widget.contentPadding ??
                EdgeInsets.fromLTRB(12.w, 4.h, 12.w, 4.h),
            border: InputBorder.none,
            isDense: true,
          ),
    );
  }
}
